package com.zb.springcloud.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.zb.springcloud.entities.CommonResult;
import com.zb.springcloud.entities.Payment;
import com.zb.springcloud.myhandler.CustomerBlockHandler;
import com.zb.springcloud.service.SentinelService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
public class RateLimitController {
    @Resource
SentinelService sentinelService;
    @GetMapping("/byResource")
    @SentinelResource(value = "byResource", blockHandler = "handleException")
    public CommonResult byResource(){
        return  new CommonResult(200, "按资源名称限流测试OK", new Payment(2020L, "serial001"));
    }

    public CommonResult handleException(BlockException e){
        return  new CommonResult(444, e.getClass().getCanonicalName()+"\t  服务不可用");
    }
    @GetMapping("/customerBlockHandler")
    @SentinelResource(value = "customerBlockHandler",blockHandlerClass = CustomerBlockHandler.class,blockHandler = "handlerException")
    public CommonResult customerBlockHandler(){
        return  new CommonResult(200, "按客户自定义", new Payment(2020L, "serial003"));
    }
    @GetMapping("/customerBlockHandler2")
    @SentinelResource(value = "customerBlockHandler2",blockHandlerClass = CustomerBlockHandler.class,blockHandler = "handlerException2")
    public CommonResult customerBlockHandler2(){
        return  new CommonResult(200, "按客户自定义", new Payment(2020L, "serial002"));
    }
    @GetMapping("/a")
    @SentinelResource(value = "controllerSentinel",blockHandler = "handler")
    public String tetsA(){
        System.out.println("进入了接口");
        String test = sentinelService.test();
        return test + "hahah";
    }

    public String handler(BlockException e){
        return "这是没进入接口的限流";
    }
}
